#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/input.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h> //mmap

enum Direction{
    UP=0, DOWN, LEFT, RIGHT,CLICK
};

 int red = 0x00ff0000;
 int green = 0x0000ff00;
 int blue = 0x000000ff;

int show_circle(int *addr,int m,int n,int r,int color)
{
    //bg_color
    int white = 0x00ffffff;

    //直接通过mmap这个函数映射的地址来给LCD刷颜色
    int x = 0; //横轴
    int y = 0; //纵轴
    for(y=0;y<480;y++)
    {
        //通过映射的地址来指向LCD上每一个像素点，然后解引用赋值
        for(x=0;x<800;x++)
        {
            //背景色刷白色
            *(addr+y*800+x) = white;

            //画了一个圆心是(400,240)半径是30的圆面
            if((x-m)*(x-m)+(y-n)*(y-n) <= r*r)
                *(addr+y*800+x) = color;
            
            //画另外一个圆
            // if((x-400)*(x-400)+(y-240)*(y-240) <= 30*30)
            //     *(addr+y*800+x) = red; 
        }
    }    
}

int Get_Slide_or_CLICK(int *x, int *y)
{

    #if 1
    //触摸屏滑动
        //打开触摸屏文件
        int fd_ts = open("/dev/input/event0", O_RDWR);
        if (fd_ts < 0)
        {
            perror("open /dev/input/event0 fail");
            exit(1);
        }
        struct input_event ts;

     
        int x1=0,y1=0;
        //抓取按下值
        while(1)
        {   
            memset(&ts, 0, sizeof(struct input_event));
            read(fd_ts, &ts, sizeof(struct input_event));
            //按下时，计算X的坐标
            if(ts.type == EV_ABS && ts.code == ABS_X)
            {
                *x = ts.value * 800 / 1024;
            }

            //按下时，计算Y坐标
            if(ts.type == EV_ABS && ts.code == ABS_Y)
            {
                *y = ts.value * 480 / 600;
            }

            //按下时的坐标
            if(ts.type == EV_KEY && ts.value == 1 && ts.code == BTN_TOUCH)
            {
                printf("X=%d  Y=%d \n", *x, *y);
                break;
            }

        }


        //抓取松开值
        while(1)
        {   
            memset(&ts, 0, sizeof(struct input_event));
            read(fd_ts, &ts, sizeof(struct input_event));
            //松开时，计算X的坐标
            if(ts.type == EV_ABS && ts.code == ABS_X)
            {
                x1 = ts.value * 800 / 1024;
            }

            //松开时，计算Y坐标
            if(ts.type == EV_ABS && ts.code == ABS_Y)
            {
                y1 = ts.value * 480 / 600;
            }

            //松开时的坐标
            if(ts.type == EV_KEY && ts.value == 0 && ts.code == BTN_TOUCH)
            {
                printf("X1=%d  Y1=%d \n", x1, y1);
                break;
            }

        

        //判断左右
        //可以给x与x1,y与y1做差，排除误触的可能
        if(x1 == 0) printf("CLICK\n");    
        else if(*x > x1 && *y == y1) printf("正左划\n");
        else if(*x > x1 && *y > y1) printf("左上划\n");
        else if(*x > x1 && *y < y1) printf("左下划\n");
        else if(*x < x1 && *y == y1) printf("正右划\n");
        else if(*x < x1 && *y > y1) printf("右上划\n");
        else  printf("右下划\n");
        
    }
    #endif
}

int main(int argc, char **argv)
{

    //打开LCD文件
    int fd_lcd = open("/dev/fb0",O_RDWR);
    if(fd_lcd < 0)
    {
        printf("open lcd fail\n");
        return -1;
    }

    //LCD的映射
    int *addr = mmap(NULL, //If addr is NULL, then the kernel chooses the address
                     800*480*4, //LCD的大小
                     PROT_READ|PROT_WRITE,//LCD的权限
                     MAP_SHARED,//标志量
                     fd_lcd, //LCD的文件描述符
                     0); //LCD的偏移量
    if(addr == NULL)
    {
        printf("mamp fail\n");
        return -1;
    }

    
    //画按钮
    show_circle(addr,400,240,100,green);

    int x=0, y=0,flag=0;
    while(1)
    {
        Get_Slide_or_CLICK(&x, &y);
        //在按钮有效范围内点击则有效
        if(((x-400)*(x-400)+(y-240)*(y-240))<=10000)
        {
            if(flag == 0)
            {
                show_circle(addr,400,240,100,red);
                flag = 1;
            }
            else
            {
                show_circle(addr,400,240,100,green);
                flag = 0;
            }
        }
    }

    return 0;
}